package com.guigu.dealservice.service.impl;

import ch.qos.logback.core.status.StatusUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.dealservice.dto.EquipmentType;
import com.guigu.dealservice.entity.PurchaseDemand;
import com.guigu.dealservice.feign.MyEquipmentFeign;
import com.guigu.dealservice.feign.MyUserFeign;
import com.guigu.dealservice.mapper.PurchaseDemandMapper;
import com.guigu.dealservice.service.PurchaseDemandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dealservice.vo.PurchaseDemandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 求购信息表（存储买家发布的二手机械求购需求） 服务实现类
 * </p>
 *
 * @author ysp
 * @since 2025-10-10
 */
@Service
public class PurchaseDemandServiceImpl extends ServiceImpl<PurchaseDemandMapper, PurchaseDemand> implements PurchaseDemandService {
    @Autowired
    private MyEquipmentFeign myEquipmentFeign;
    @Autowired
    private MyUserFeign myUserFeign;



    @Override
    public R queryPurchaseDemandByPage(int pageNum, int pageSize, PurchaseDemandVo vo) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<PurchaseDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(vo.getStatus()!=null,"purchase_status",vo.getStatus());
        queryWrapper.eq(vo.getType()!=null,"type_id",vo.getType());
        if (vo.getReleaseTime() != null && vo.getReleaseTime().size() >= 2) {
            queryWrapper.ge(vo.getReleaseTime().get(0) != null , "release_time", vo.getReleaseTime().get(0));
            queryWrapper.le(vo.getReleaseTime().get(1) != null , "release_time", vo.getReleaseTime().get(1));
        }
        List<PurchaseDemand> purchaseDemands = this.list(queryWrapper);
        for (PurchaseDemand p:purchaseDemands) {
            p.setEquipmentType(myEquipmentFeign.queryEquipmentTypeById(p.getTypeId()));
            p.setUser(myUserFeign.queryUserById(p.getUserId()));
        }
        PageInfo<PurchaseDemand> pageInfo = new PageInfo<>(purchaseDemands);
        return R.ok().data("pageInfo",pageInfo);
    }


    @Override
    public R allocatePurchaseDemand(Integer id, Long businessmanId) {
        PurchaseDemand purchaseDemand = this.getById(id);
        if (purchaseDemand != null) {
            purchaseDemand.setBusinessmanId(businessmanId);
            purchaseDemand.setPurchaseStatus(1);
        }
        this.updateById(purchaseDemand);
        return R.ok();
    }

    @Override
    public PageInfo<PurchaseDemand> queryPurchaseDemandByPagePC(Integer pageNum, Integer pageSize, PurchaseDemandVo purchaseDemandVo) {
       PageHelper.startPage(pageNum, pageSize);
       QueryWrapper<PurchaseDemand> queryWrapper = new QueryWrapper<>();
       if (purchaseDemandVo != null){
           Integer purchaseStatus = purchaseDemandVo.getPurchaseStatus();
           if (purchaseStatus!=null){
               queryWrapper.eq("purchase_status",purchaseStatus);
           }
           if (!purchaseDemandVo.getBrand().isEmpty()){
               queryWrapper.like("brand",purchaseDemandVo.getBrand());
           }
       }
       queryWrapper.orderByDesc("release_time");
       List<PurchaseDemand> purchaseDemands = this.list(queryWrapper);
       for (int i=0;i<purchaseDemands.size();i++){
           PurchaseDemand purchaseDemand = purchaseDemands.get(i);
           // 根据设备typeId查类型名称
           EquipmentType equipmentType = myEquipmentFeign.queryEquipmentTypeById_PC(purchaseDemand.getTypeId());
           purchaseDemand.setTypeName(equipmentType.getTypeName());
       }
        PageInfo<PurchaseDemand> purchaseDemandPageInfo = new PageInfo<>(purchaseDemands);
        return purchaseDemandPageInfo;
    }

    @Override
    public R addPurchaseDemand(PurchaseDemand purchaseDemand) {
        purchaseDemand.setCreateTime(LocalDateTime.now());
        purchaseDemand.setUpdateTime(LocalDateTime.now());
        purchaseDemand.setReleaseTime(LocalDateTime.now());
        purchaseDemand.setPurchaseStatus(0);
        boolean save = this.save(purchaseDemand);
        return R.ok().success(save);
    }

    @Override
    public PageInfo<PurchaseDemand> queryPurchaseDemandByPageUserIdPC(Integer pageNum, Integer pageSize, Long userId, PurchaseDemandVo purchaseDemandVo) {
       PageHelper.startPage(pageNum, pageSize);
       QueryWrapper<PurchaseDemand> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("user_id",userId);
       if (purchaseDemandVo != null){
           Integer purchaseStatus = purchaseDemandVo.getPurchaseStatus();
           if (purchaseStatus!=null){
               queryWrapper.eq("purchase_status",purchaseStatus);
           }
       }
       List<PurchaseDemand> purchaseDemands =this.list(queryWrapper);
       for (int i=0;i<purchaseDemands.size();i++){
           PurchaseDemand purchaseDemand = purchaseDemands.get(i);
           // 根据设备typeId查类型名称
           EquipmentType equipmentType = myEquipmentFeign.queryEquipmentTypeById_PC(purchaseDemand.getTypeId());
           purchaseDemand.setTypeName(equipmentType.getTypeName());
       }
        PageInfo<PurchaseDemand> objectPageInfo = new PageInfo<>(purchaseDemands);
        return objectPageInfo;
    }

    @Override
    public R selectPurchaseByEId(Integer bid) {
        QueryWrapper<PurchaseDemand> wrapper = new QueryWrapper<>();
        wrapper.eq("businessman_id",bid);
        wrapper.eq("purchase_status",1);
        List<PurchaseDemand> list = this.list(wrapper);
        for (PurchaseDemand p:list) {
            p.setUser(myUserFeign.queryUserById(p.getUserId()));
            p.setEquipmentType(myEquipmentFeign.queryEquipmentTypeById(p.getTypeId()));
        }
        PageInfo<PurchaseDemand> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageInfo",pageInfo);
    }

    @Override
    public R selectPurchaseById(Integer id) {
        QueryWrapper<PurchaseDemand> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_id",id);
        PurchaseDemand purchaseDemand = this.getOne(wrapper);
        purchaseDemand.setUser(myUserFeign.queryUserById(purchaseDemand.getUserId()));
        purchaseDemand.setEquipmentType(myEquipmentFeign.queryEquipmentTypeById(purchaseDemand.getTypeId()));
        return R.ok().data("list",purchaseDemand);
    }

    @Override
    public R queryPurchaseDemandByUserId(Integer pageNum, Integer pageSize,PurchaseDemandVo  vo) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<PurchaseDemand> queryWrapper = new QueryWrapper<>();
        //求购用户
        queryWrapper.eq("user_id", vo.getUserId());
        //求购状态
        queryWrapper.eq(vo.getStatus()!=null,"purchase_status",vo.getStatus());
        //求购类型
        queryWrapper.eq(vo.getType()!=null,"equipment_type",vo.getEquipmentType());
        //求购品牌
        queryWrapper.eq(vo.getBrand()!=null,"brand",vo.getBrand());
        //求购型号
        queryWrapper.eq(vo.getModel()!=null,"model",vo.getModel());
        List<PurchaseDemand> purchaseDemands = this.list(queryWrapper);
        for (PurchaseDemand p:purchaseDemands) {
            p.setEquipmentType(myEquipmentFeign.queryEquipmentTypeById(p.getTypeId()));
            p.setUser(myUserFeign.queryUserById(p.getUserId()));
        }
        PageInfo<PurchaseDemand> pageInfo = new PageInfo<>(purchaseDemands);
        return R.ok().data("pageInfo",pageInfo);
    }

    @Override
    public R deletePurchaseDemandByBidAndPid(Integer uid, Integer pid) {
        QueryWrapper<PurchaseDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        queryWrapper.eq("purchase_id",pid);
        boolean remove = this.remove(queryWrapper);
        if (remove) {
            return R.ok().message("删除成功！！！");
        }else{
            return R.error().message("删除失败！！！");
        }
    }

    @Override
    public R updatePurchaseDemandByBidAndPid(PurchaseDemand purchaseDemand) {
        purchaseDemand.setUpdateTime(LocalDateTime.now());
        boolean update = this.updateById(purchaseDemand);
        if (update) {
            return R.ok().message("修改成功！！！");
        }else return R.error().message("修改失败！！！");
    }


}
